package com.gas.gm.pdu.handler;

import java.io.Serializable;

import com.gas.gm.model.Account;
import com.gas.gm.service.AccountService;
import com.gas.gm.util.GasContextHelper;
import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;

public class BindRequestHandler extends GasRequestHandler {
	
	private Account serializable;
	
	private boolean valid=true;
	
	private AccountService service;

	public boolean isValidAccount() {
		return valid;
	}
	

	public AccountService getService() {
		if(service==null){
			service=GasContextHelper.getApplicationContext().getBean(AccountService.class);
		}
		return service;
	}

	@Override
	public Serializable getSerializableObject() {
		if(valid)
			return serializable;
		else
			return null;
		
	}

	@Override
	public int validateBindRequest(BindRequest r) {
		int commandStatus=Data.ESME_ROK;
		serializable=getService().findFromFile(r.getSystemId());
		String password=null;
		if(serializable!=null){
			password=serializable.getPassword();
			if(password!=null){
				if(!password.equals(r.getPassword())){
					commandStatus=Data.ESME_RINVPASWD;
					valid=false;
				}else{
					valid=true;
				}
			}else{
				commandStatus=Data.ESME_RINVPASWD;
				valid=false;
			}
		}else{
			commandStatus=Data.ESME_RINVSYSID;
			valid=false;
		}
		return commandStatus;
	}

	
}
